package com.ts.animal.controller;

import com.ts.animal.domain.ExWarningSetDO;
import com.ts.animal.domain.WarningSetDO;
import com.ts.animal.service.WarningSetService;
import com.ts.common.utils.PageUtils;
import com.ts.common.utils.Query;
import com.ts.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 预警配置表
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2022-09-02 16:18:40
 */

@Controller
@RequestMapping("/animal/warningSet")
public class WarningSetController {
    @Autowired
    private WarningSetService warningSetService;

    @GetMapping()
    @RequiresPermissions("animal:warningSet:warningSet")
    String WarningSet() {
        return "animal/warningSet/warningSet";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("animal:warningSet:warningSet")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<WarningSetDO> warningSetList = warningSetService.list(query);
        int total = warningSetService.count(query);
        PageUtils pageUtils = new PageUtils(warningSetList, total);
        return pageUtils;
    }

    @ResponseBody
    @RequestMapping("/listAll")
    @RequiresPermissions("animal:warningSet:warningSet")
    public Result listAll() {
        //查询列表数据
        List<ExWarningSetDO> warningSetList = warningSetService.listAll();
        return Result.ok().setData(warningSetList);
    }

    @GetMapping("/add")
    @RequiresPermissions("animal:warningSet:add")
    String add() {
        return "animal/warningSet/add";
    }

    @GetMapping("/edit/{animalType}")
    @RequiresPermissions("animal:warningSet:edit")
    String edit(@PathVariable("animalType") String animalType, Model model) {
        WarningSetDO warningSet = warningSetService.get(animalType);
        model.addAttribute("warningSet", warningSet);
        return "animal/warningSet/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("animal:warningSet:add")
    public Result save(@RequestBody List<ExWarningSetDO> exWarningSets) {
        Result result = Result.ok();
        warningSetService.save(result, exWarningSets);
        return result;
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("animal:warningSet:edit")
    public Result update(WarningSetDO warningSet) {
        Result result = Result.ok();
        warningSetService.update(result, warningSet);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("animal:warningSet:remove")
    public Result remove(String animalType) {
        Result result = Result.ok();
        warningSetService.remove(result, animalType);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("animal:warningSet:batchRemove")
    public Result remove(@RequestParam("ids[]") String[] animalTypes) {
        Result result = Result.ok();
        warningSetService.batchRemove(result, animalTypes);
        return result;
    }

}
